﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Concurrent;

namespace MCB.Net.Utility
{
    /// <summary>
    /// A pool containing a number of objects for use
    /// </summary>
    public class Pool<T>
    {
        protected ConcurrentStack<T> _items = new ConcurrentStack<T>();

        /// <summary>
        /// Constructor
        /// </summary>
        public Pool()
        {
        }

        /// <summary>
        /// Removes an availble item from the pool
        /// </summary>
        /// <returns></returns>
        public T Remove()
        {
            T item;
            if (_items.TryPop(out item))
                return item;
            else
                return default(T);
        }

        /// <summary>
        /// Add an item to a availble pool
        /// </summary>
        public void Add(T item)
        {
            _items.Push(item);
        }
    }
}
